$(function () {
    $("#receiverLat").val($("#receiverLat").val() / 1000000);
    $("#receiverLng").val($("#receiverLng").val() / 1000000);

    var step1Text = '填写商店信息';
    var orderStatus = $('[name="status"]').val();
    if (orderStatus != '3' && orderStatus != '4') {
        $("#orderStatus").loadStep({
            html:true,
            size: "large",
            color: "blue",
            steps: [{
                title: "已下单",
                content: "<span style='font-size:14px;'>"+$("#placeOrderTime").val()+'</span>'+"<br><span style='font-size:14px;'>买家已下单，等待卖家发货</span>",
                margin: -14,
                html:true
            }, {
                title: "已发货",
                content: (orderStatus==1||orderStatus==2?"<span style='font-size:14px;'>"+$("#deliverGoodsTime").val()+'</span>'+"<br>":"")+"<span style='font-size:14px;'>卖家已发货，等待买家收货</span>",
                margin: -3
            }, {
                title: "已收货",
                content: (orderStatus==2?"<span style='font-size:14px;'>"+$("#receiveGoodsTime").val()+'</span>'+"<br>":"")+"<span style='font-size:14px;'>买家已收货，订单完成</span>",
                margin: -12
            }]
        });
        if (orderStatus == '0')
            $("#orderStatus").setStep(1);
        else if (orderStatus == '1')
            $("#orderStatus").setStep(2);
        else if (orderStatus == '2')
            $("#orderStatus").setStep(3);
    }
    $('#total').html("¥" + $("#orderSum").val());
    $.ajax({
        url: ctx + "/order/goodList.do",
        data: {orderId: $('[name="orderId"]').val()},
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                $('.info-tijiao').before('' +
                    '<div class="cart-con-info" style="margin-top: 12px;">' +
                    getHtml(data[i]) +
                    '</div>');
            }
        }
    });
});

function mapCallback() {
    var map = new BMap.Map("map");

    map.addControl(new BMap.MapTypeControl());   //添加地图类型控件
    map.addControl(new BMap.NavigationControl());//左上角，添加默认缩放平移控件
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
    var point = new BMap.Point($("#receiverLng").val(), $("#receiverLat").val());
    map.setCurrentCity($('#receiverProvince').val());
    map.centerAndZoom(point, 16);
    var marker = new BMap.Marker(point);// 创建标注
    map.addOverlay(marker);// 将标注添加到地图中
    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
    var markerWindowContent = "<p style='font-size: 16px;margin-bottom: 5px;'>收货地址</p><font style='font-size:15px;color: #3276b1;'>" + $("#receiverAddress").val() + "</font>";
    var markerWindow = new BMap.InfoWindow(markerWindowContent);  // 创建信息窗口对象
    //marker.openInfoWindow(markerWindow);
    marker.addEventListener("click", function () {
        this.openInfoWindow(markerWindow);
    });
}

function getHtml(good) {
    var comment;
    if(!good.commentId)//没有评论
        comment = ($('[name="status"]').val()==2?'<p class="mid-je f-l">' + '<a class="viewOrder del_item block" style="padding-right: 51px;" href="'+ctx+'/order/evaluatePage.do?id='+good.id+'">评论</a>' + '</p>':"");
    else
        comment = '<p class="mid-je f-l">' + '<a class="viewOrder del_item block" style="padding-right: 51px;">已评论</a></p>';
    return '' +
        '<div class="info-mid">' +
        '<div class="mid-tu f-l" style="margin-left: 30px;">' +
        '<a><img style="cursor:pointer;" height="80" width="80" src="' + ctx + '/resource/download.do?id=' + good.photo + '"/></a>' +
        '</div>' +
        '<div class="mid-font f-l">' +
        '<a>' + good.name + '</a>' +
        '</div>' +
        '<p class="mid-dj f-l">' + good.count + '</p>' +
        '<p class="mid-dj f-l">' + good.price + '</p>' +
        '<p class="mid-je f-l">' + good.sum + '</p>' +
        comment +
        '<div style="clear:both;"></div>' +
        '</div>';
}

function selectSender(li) {
    if ($(li).hasClass("current"))
        return;
    else {
        $(li).addClass("current");
        $(li).siblings('.current').removeClass("current");
    }
}

function confirmDeliver() {
    if ($('[name="senderLi"]').length == 0) {
        alert("请选择送货人");
        return;
    }
    var senderId = $('[name="senderLi"].current [name="senderId"]').val();
    var senderName = $('[name="senderLi"].current [name="senderName"]').val();
    confirm('<div style="line-height: 33px;">是否确定将'+senderName+'选为送货人，并确认发货？</div>', function(result) {
        if(result=="yes"){
            $.ajax({
                url: ctx + "/order/deliverGood.do",
                data: {id: $("[name='orderId']").val(),sender:senderId},
                success: function (data) {
                    if (data.flag > 0) {
                        location.href = ctx+"/order/orderDetail.do?id="+$("[name='orderId']").val();
                    }else{
                        alert("操作失败");
                    }
                }
            });
        }
    });
}